package com.authenticateCenter.assembler;


import com.authenticateCenter.domain.entity.User;
import com.authenticateCenter.domain.vo.UserVo;
import com.authenticateCenter.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.mybaits.CustomPage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import static com.authenticateCenter.factory.UserFactory.USER_CONVERT;

/**
 * @author 大忽悠
 * @create 2022/10/8 21:29
 */
@Component
@RequiredArgsConstructor
public class UserAssembler implements InfoAssembler<UserVo,QueryWrapper<User>,QueryWrapper<User>>{
    private final IUserService iUserService;
    private final RoleAssembler roleAssembler;

    /**
     * @param queryWrapper
     * @return 返回组装的结果
     */
    @Override
    public UserVo assembler(QueryWrapper<User> queryWrapper) {
        User user = iUserService.getOne(queryWrapper);
        if(user==null){
            return null;
        }
        UserVo userVo=USER_CONVERT.toUserVo(user);
        userVo.setRoles(roleAssembler.assemblers(null,userVo.getId()));
        return userVo;
    }

    /**
     * @param queryWrapper
     * @return 返回组装的多个结果
     */
    @Override
    public List<UserVo> assemblers(CustomPage customPage, QueryWrapper<User> queryWrapper) {
        List<User> users = CustomPage.getResult(customPage, new User(), iUserService, queryWrapper);
        if(users==null || users.isEmpty()){
            return null;
        }
        List<UserVo> userVos = USER_CONVERT.toUserVoList(users);
        userVos.forEach(userVo -> userVo.setRoles(roleAssembler.assemblers(null,userVo.getId())));
        return userVos;
    }
}
